package com.quyeying.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.quyeying.annotation.PassToken;
import com.quyeying.entity.TblAddressEntity;
import com.quyeying.service.TblAddressService;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
* <p>
* tbl_address 前端控制器
* </p>
* @author jll
* @since 2022-07-25
*/
@Slf4j
@RestController
@RequestMapping("addressEntity")
public class TblAddressController {

    @Resource
    private TblAddressService tblAddressService;

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(@RequestBody TblAddressEntity tblAddressEntity,HttpServletRequest request){
        try {
            return tblAddressService.add(tblAddressEntity,request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return tblAddressService.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody TblAddressEntity tblAddressEntity,HttpServletRequest request){
        try {
            return tblAddressService.updateData(tblAddressEntity,request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 分页查询
    **/
    @GetMapping("/findListByPage")
    public R findListByPage(@RequestParam Integer current,
                            @RequestParam Integer size, HttpServletRequest request){
        try {
            return tblAddressService.findListByPage(current, size, request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @GetMapping("/findById")
    public R findById(Long id){
        try {
            return tblAddressService.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * id查询
     **/
    @GetMapping("/findByIdOrDefault")
    public R findByIdOrDefault(Long id,HttpServletRequest request){
        try {
            return tblAddressService.findByIdOrDefault(id, request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 通过code得到默认的收货地址
     * @param codes
     * @return
     */
    @PassToken
    @GetMapping("/findByCodeDefault")
    public R findByCodeDefault(@RequestParam String codes) {
        try {
            return tblAddressService.findByCodeDefault(codes);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }
}
